import {
  userInfo
} from '../../util/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //先定义空对象 初始化
    userInfo: {},
    isShow: true,
    isShowimg: false,
    portrait: [], //头像
    username: "", //头像昵称
    balance: 0.00, //余额
    integral: 0, //积分
    bankCard: 0, //卡
    coupons: 0, //优惠码


  },
  //授权登陆按钮
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      //用户授权登陆，并跳转首页
      wx.login({
        success: function (res) {
          var data = {
            "encryptedData": e.detail.encryptedData,
            "signature": e.detail.signature,
            "rawData": e.detail.rawData,
            " iv": e.detail.iv,
            "code": res.code
          };

          // 请求自己后台获取用户openid
          wx.request({
            url: 'https://api.zhouyfeng.cn/?s=Login.WxDecode',
            method: 'POST',
            dataType: "json",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data,
            success: function (res) {
              var json = res.data.data;
              var formData = {
                open_id: json.open_id
              }    
              setTimeout(function () {
                userInfo(formData).then((res => {
                  if (res.data.ret == 200) {
  
                    var dataInfo = res.data.data;
                  console.log(dataInfo);
           
                  
                    wx.setStorage({
                      key: "userInfo",
                      data: dataInfo
                    });
                    wx.setStorage({
                      data: dataInfo.avatarUrl,
                      key: 'portrait',
                    });
                    wx.setStorage({
                      data: dataInfo.phone,
                      key: 'phone',
                    });
                    wx.setStorage({
                      data: dataInfo.gender,
                      key: 'gender',
                    });
                    
                   //判断上次登录是否修改昵称
                    if (dataInfo.username != null) {
                      that.setData({
                        username: dataInfo.username,
                      });
                      wx.setStorage({
                        data: dataInfo.username,
                        key: 'username',
                      });
                    } else {
                      that.setData({
                        username: dataInfo.nickName,
                      });
                      wx.setStorage({
                        data: dataInfo.nickName,
                        key: 'username',
                      });
                    };
                    that.setData({
          
                      userInfo: dataInfo, //获取回来的用户信息
                      integral: dataInfo.integral,
                      portrait: dataInfo.avatarUrl, //头像
                      isShow: false, //登录键隐藏
                      isShowimg: true //显示用户图片
                    })
                  }
                }))
              }, 2000) 
            

            },
            fail: function () {
              // that.showModal('获取授权信息失败')
          
            },
         
          })
        }
      })
    } else {
      // //用户按了拒绝授权按钮

    }

  },


  getuserinfos() {
  
    //判断用户是否授权
    wx.getSetting({ //得到设置
      success: (data) => {
        if (data.authSetting['scope.userInfo']) {
          console.log("22");
          var data = wx.getStorageSync('userInfo');
          var portrait = wx.getStorageSync('portrait');
          var username = wx.getStorageSync('username');
     
          //用户已经授权 隐藏按钮
          this.setData({ //设置data
            integral: data.integral,
            portrait: portrait, //头像
            username: username, //昵称
            isShow: false, //登录键隐藏
            isShowimg: true //显示用户图片
          })
        } else{
          
          
        }
       
      },
      
    
    })
    //获取用户登陆的信息
    wx.getUserInfo({
      success: (data) => { //当前对象 回调这样写
        this.setData({ //设置data
          //上面定义的空对象拿到data里面的信息
          userInfo: data.userInfo
        })
      }
    })
  },
//获取用户信息
  getUser() {
    var user = wx.getStorageSync('userInfo');
    var formData = {
      open_id: user.open_id
    }
    userInfo(formData).then((res => {
      if (res.data.ret == 200) {

        this.setData({
          portrait: res.data.data.avatarUrl
        })
        wx.setStorage({
          data: res.data.data.avatarUrl,
          key: 'portrait',
        })
      }
    }))
  },
  //获取用户昵称
  getUserName() {
    var user = wx.getStorageSync('userInfo');
   
    var formData = {
      open_id: user.open_id
    }
    userInfo(formData).then((res => {
      if (res.data.ret == 200) {
        this.setData({
          username: res.data.data.username
        })
        wx.setStorage({
          data: res.data.data.username,
          key: 'username',
        })
      }
    }))
  },
  changeData: function () {
    this.getUser(); //刷新的区域的代码
    this.getUserName();
   
  },


  //积分
  toIntegral: function () {
    wx.navigateTo({
      url: '../integral/integral'
    })
  },
  //卷码
  toPromoCode: function () {
    wx.navigateTo({
      url: '../promoCode/promoCode'
    })
  },
  //卡
  toEquityCard: function () {
    wx.navigateTo({
      url: '../equityCard/equityCard'
    })
  },

  //订单
  toEvaluation: function () {
    wx.navigateTo({
      url: '../evaluation/evaluation'
    })
  },
  //售后
  toAfterSsales: function () {
    wx.navigateTo({
      url: '../afterSsales/afterSsales'
    })
  },
  //个人信息
  toPersonalInfo: function () {
    wx.navigateTo({
      url: '../personalInfo/personalInfo'
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getuserinfos(); //直接加载这个方法

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  this.onLoad(); //重新加载onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})